local function bool(v) return not not v end

AddModRPCHandler("akemi_homura", "timepause", function(inst)
    if inst.components.homura_skill then
        inst.components.homura_skill:Trigger()
    end
end)

AddModRPCHandler("akemi_homura", "attackbutton", function(inst, pressed)
    if inst.components.playercontroller then
        inst.components.playercontroller.lw_attackpressed = bool(pressed)
    end
end)

AddModRPCHandler("akemi_homura", "exitsniping", function(inst)
    if inst.components.homura_sniper and inst.components.homura_sniper:IsSniping() then 
        inst.components.homura_sniper:StopSniping()
    end 
end)

AddPlayerPostInit(function(inst) 
    local _keydown = false
    local function OnControl(key, down)
    
        if (key == CONTROL_ATTACK or key == CONTROL_CONTROLLER_ATTACK)
            and ThePlayer == inst and inst.components.playercontroller then
            --这句判定其实没什么必要, 因为只有ThePlayer会带有玩家控制组件.

            if _keydown == (down == true) then --脏标
                return true
            end
            _keydown = (down == true)

            if TheWorld.ismastersim then
                inst.components.playercontroller.lw_attackpressed = (down == true)
            else
                SendModRPCToServer( MOD_RPC.akemi_homura.attackbutton, (down == true))
            end
        end
    end
    TheInput:AddGeneralControlHandler(function(ctrl, down) OnControl(ctrl, down) end)
end)

AddModRPCHandler("akemi_homura", "mouseovertarget", function(inst, ent)
    if inst.components.playercontroller then
        inst.components.playercontroller.lw_mouseover = ent
    end
end)

AddComponentPostInit('playercontroller', function(self)
    local old_ent = nil
    local old_update = self.OnUpdate
    function self:OnUpdate(...)
        old_update(self, ...)

        local ent = TheInput:GetWorldEntityUnderMouse()
        if ent ~= old_ent then  --脏标
            old_ent = ent
            if TheWorld.ismastersim then
                self.lw_mouseover = ent 
            else
                SendModRPCToServer( MOD_RPC.akemi_homura.mouseovertarget, ent)
            end
        end
    end
end)
